package com.fireline.beauty.member;

import java.math.BigDecimal;
import java.math.MathContext;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.layout.RowData;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.wb.swt.SWTResourceManager;
import com.zgw.fireline.base.widgets.Batch;
import com.zgw.fireline.base.widgets.ControlUtil;
import com.zgw.fireline.base.IDataBaseProvide;
import com.fireline.beauty.ContextHelp;
import com.fireline.beauty.jdbc.SqliteJdbcImpl;
import com.zgw.fireline.base.widgets.BatchItem;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Combo;

/**
 * 付款窗口
 * */
public class FkDialog extends Dialog {

	/**
	 * @wbp.nonvisual location=13,502
	 */
	private final Batch batch = new Batch(SqliteJdbcImpl.INSTANCE);

	protected int result;
	protected Shell shell;
	private Text textJfje;
	public String fkyh, fkqk, fkfs, jfje, yfje;
	private Button buttonXj;

	private Button buttonXyk;

	private Button buttonDjq;

	private Button buttonCzk;

	private CLabel labZlje;

	private int lb = 0;// 0 储值卡充值 1套餐续费 2 套餐退费 3还款

	static DecimalFormat dFormat = new DecimalFormat("0.00");

	/**
	 * Create the dialog.
	 * 
	 * @param parent
	 * @param style
	 */
	public FkDialog(Shell parent, int style, int lb) {
		super(parent, style);
		setText("SWT Dialog");
		this.lb = lb;
	}

	/**
	 * Open the dialog.
	 * 
	 * @return the result
	 */
	public int open() {
		createContents();
		ControlUtil.centerShell(shell);
		shell.open();
		shell.layout();
		Display display = getParent().getDisplay();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
		return result;
	}

	/**
	 * Create contents of the dialog.
	 */
	private void createContents() {

		BatchItem batchItem = new BatchItem(batch);
		batchItem
				.setCommand(" insert into hyjf(id,hybh,rq,czy,yfje,jfje,yhje,sjje,yh,fkyh,qk,fkqk,zt,ztsm,tcbh,dqje,xj,xyk,djq,czk,bz,ygbh,fd)\r\n       values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
		batchItem.setName("添加交费记录");

		BatchItem batchItem_1 = new BatchItem(batch);
		batchItem_1.setCommand("update hyxx set knje=?\r\nwhere hybh=?");
		batchItem_1.setName("修改会员卡佘额");

		shell = new Shell(getParent(), SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);
		shell.setSize(435, 326);
		shell.setText("付款");
		GridLayout gl_shell = new GridLayout(1, false);
		gl_shell.marginWidth = 15;
		shell.setLayout(gl_shell);

		Composite composite = new Composite(shell, SWT.NONE);
		composite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false,
				1, 1));
		GridLayout gl_composite = new GridLayout(2, false);
		gl_composite.marginHeight = 20;
		gl_composite.verticalSpacing = 20;
		gl_composite.marginWidth = 50;
		composite.setLayout(gl_composite);

		Group group = new Group(shell, SWT.NONE);
		group.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1,
				1));
		group.setText("付款方式");
		RowLayout rl_group = new RowLayout(SWT.HORIZONTAL);
		rl_group.justify = true;
		rl_group.fill = true;
		rl_group.spacing = 10;
		rl_group.marginTop = 10;
		group.setLayout(rl_group);

		buttonXj = new Button(group, SWT.RADIO);
		buttonXj.setSelection(true);
		buttonXj.setFont(SWTResourceManager.getFont("宋体", 9, SWT.NORMAL));
		buttonXj.setText("现金");

		buttonXyk = new Button(group, SWT.RADIO);
		buttonXyk.setText("信用卡");

		buttonDjq = new Button(group, SWT.RADIO);
		buttonDjq.setText("代金券");

		buttonCzk = new Button(group, SWT.RADIO);
		buttonCzk.setEnabled(lb != 0);
		buttonCzk.setText("储值卡");
		group.setTabList(new Control[] { buttonXj, buttonXyk, buttonDjq,
				buttonCzk });

		Label lblNewLabel = new Label(composite, SWT.NONE);
		lblNewLabel.setFont(SWTResourceManager.getFont("华文细黑", 14, SWT.BOLD));
		lblNewLabel.setText("交费金额");

		CLabel lblNewLabel_1 = new CLabel(composite, SWT.NONE);
		lblNewLabel_1.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false,
				false, 1, 1));
		lblNewLabel_1.setFont(SWTResourceManager.getFont("宋体", 20, SWT.BOLD));
		lblNewLabel_1.setForeground(SWTResourceManager.getColor(SWT.COLOR_RED));
		lblNewLabel_1.setText(DecimalFormat.getCurrencyInstance().format(
				new BigDecimal(yfje)));

		Label lblNewLabel_2 = new Label(composite, SWT.NONE);
		lblNewLabel_2.setFont(SWTResourceManager.getFont("华文细黑", 14, SWT.BOLD));
		lblNewLabel_2.setText("付款金额");

		textJfje = new Text(composite, SWT.BORDER);
		textJfje.setForeground(SWTResourceManager.getColor(SWT.COLOR_RED));
		ControlUtil.setCurrencyRule(textJfje, false, false);
		ControlUtil.setEnterToFocusMove(textJfje);
		textJfje.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				doInput();
			}
		});
		textJfje.selectAll();
		textJfje.setFont(SWTResourceManager.getFont("幼圆", 22, SWT.BOLD));
		textJfje.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false,
				1, 1));

		Label label_1 = new Label(composite, SWT.NONE);
		label_1.setFont(SWTResourceManager.getFont("华文细黑", 14, SWT.BOLD));
		label_1.setText("找零金额");

		labZlje = new CLabel(composite, SWT.NONE);
		labZlje.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false,
				1, 1));
		labZlje.setFont(SWTResourceManager.getFont("幼圆", 22, SWT.BOLD));
		labZlje.setForeground(SWTResourceManager.getColor(SWT.COLOR_RED));
		labZlje.setText("");

		Composite composite_1 = new Composite(shell, SWT.NONE);
		RowLayout rl_composite_1 = new RowLayout(SWT.HORIZONTAL);
		rl_composite_1.marginTop = 10;
		rl_composite_1.spacing = 10;
		composite_1.setLayout(rl_composite_1);
		composite_1.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false,
				false, 1, 1));

		Button btnNewButton_1 = new Button(composite_1, SWT.NONE);
		btnNewButton_1.setLayoutData(new RowData(80, SWT.DEFAULT));
		btnNewButton_1.setText("确定(F5)");
		btnNewButton_1.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				doOkPressed();
			}
		});
		Button btnNewButton = new Button(composite_1, SWT.NONE);
		btnNewButton.setLayoutData(new RowData(80, SWT.DEFAULT));
		btnNewButton.setText("取消(ESC)");
		shell.setTabList(new Control[] { composite, composite_1 });

		initializeData();
		setParams();

	}

	private void initializeData() {
		textJfje.setText(yfje);
		textJfje.selectAll();
	}

	/*
	 * 输入付款金额
	 */
	protected void doInput() {
		BigDecimal bigYfje = new BigDecimal(yfje);
		BigDecimal bigJfje = new BigDecimal(textJfje.getText());
		labZlje.setText(DecimalFormat.getCurrencyInstance().format(
				bigJfje.subtract(bigYfje)));
	}

	protected void doOkPressed() {

		jfje = textJfje.getText(); // 实际交费金额
		Double ce = new BigDecimal(yfje).subtract(new BigDecimal(jfje))
				.doubleValue();
		int ceType = 0;// 1 付款欠款 ,2 付款优惠
		if (ce > 0) { // 打开差额处理窗口
			CeclDialog dlg = new CeclDialog(shell, SWT.NONE, dFormat.format(ce));
			ceType = dlg.open();
			if (ceType == 0) {
				return;
			}
		}
		fkyh = ceType == 2 ? dFormat.format(ce) : "0";
		fkqk = ceType == 1 ? dFormat.format(ce) : "0";
		fkfs = buttonXj.getSelection() ? "xj" : "";
		fkfs = buttonCzk.getSelection() ? "czk" : fkfs;
		fkfs = buttonDjq.getSelection() ? "djq" : fkfs;
		fkfs = buttonXyk.getSelection() ? "xyk" : fkfs;

		// SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmssSSSS");
		// SimpleDateFormat format2 = new
		// SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		// String id = format.format(new Date());
		// batch.setParam(0, 0, id);// id
		// batch.setParam(0, 1, hybh);// hybh
		// batch.setParam(0, 2, format2.format(new Date()));// rq
		// batch.setParam(0, 3, ContextHelp.getUserName());// czy
		// batch.setParam(0, 4, yfje);// yfje 应付金额
		// batch.setParam(0, 5, jfje);// jfje 交费金额
		// batch.setParam(0, 6, yhje);// yhje 优惠赠送金额
		// batch.setParam(0, 7, sjje);// sjje 实际入帐额
		// batch.setParam(0, 8, yh); // yh 优惠
		// // fkyh 付款优惠
		// batch.setParam(0, 9, ceType == 2 ? dFormat.format(ce) : "0");
		// batch.setParam(0, 10, qk); // qk 欠款
		// // fkqk 付款欠款
		// batch.setParam(0, 11, ceType == 1 ? dFormat.format(ce) : "0");
		// batch.setParam(0, 12, lb); // zt 状态即 付款类别
		// batch.setParam(0, 13, lbsm);// ztsm 状态说明
		// batch.setParam(0, 14, "");// tcbh套餐编号
		// batch.setParam(0, 15, dqje);// dqje卡内当前金额
		// batch.setParam(0, 16, buttonXj.getSelection() ? jfje : "0");// xj 现金
		// batch.setParam(0, 17, buttonXyk.getSelection() ? jfje : "0");// xyk
		// 信用卡
		// batch.setParam(0, 18, buttonDjq.getSelection() ? jfje : "0");// djq
		// 代金券
		// batch.setParam(0, 19, "0");// czk 储值卡
		// batch.setParam(0, 20, bz);// bz 备注
		// batch.setParam(0, 21, ygbh);// ygbh 员工编号
		// batch.setParam(0, 22, "1");// fd
		//
		// // 修改会员卡内佘额信息
		// batch.setParam(1, 0, dqje);
		// batch.setParam(1, 1, hybh);
		// try {
		// batch.execute();
		// } catch (Exception e) {
		// MessageDialog.openError(shell, "程序异常", "付款失败:" + e.getMessage());
		// }
		result = SWT.OK;
		shell.close();
	}

	protected void setParams() {

	}

}
